<?php
/**
*商户账户主控制器
*/
namespace Home\Controller;
use Think\Controller;
class MerchantController extends CommonController {

	//存储商户信息
	protected $merchant_info;
	
public function _initialize(){
	parent::_initialize();
	$con['mh_id']=session('mid');
	$con['mh_isactive']=1;
	$this->merchant_info=M("merchant")->where($con)->find();
	$this->merchant_info || $this->redirect("Public/login");
	$this->assign("merchant_info",$this->merchant_info);
}
	
/**
*登录成功后的主页	
*/	
public function index(){
	$this->display();
}

/**
*修改登录密码
*/
public function modifyPwd(){
	if(md5(I("old_pwd").$this->merchant_info['mh_salt']) != $this->merchant_info['mh_pwd'] )
		$this->error(L("invalid_pwd"));
	
	if(I("old_pwd") == I("new_pwd"))
		$this->error(L("same_pwd"));
	
	if(I("new_pwd")!=I("confirm_new_pwd"))
		$this->error(L("pwd_not_match"));
	
	$con['mh_id']=$this->merchant_info['mh_id'];
	$data['mh_pwd']=md5(I("new_pwd").$this->merchant_info['mh_salt']);
	$result=M("merchant")->where($con)->save($data);
	if($result)
		$this->success(L("action_success"));
	else
		$this->error(L("action_failed"));
	
}

/**
*修改支付密码
*/
public function modifyPaypwd(){
	//对比旧的支付密码是否正确
	if(md5(I("old_pwd").$this->merchant_info['mh_salt']) != $this->merchant_info['mh_paypwd'] )
		$this->error(L("invalid_pwd"));
	//新密码与旧密码是否一样
	if(I("old_pwd") == I("new_pwd"))
		$this->error(L("same_pwd"));
	//两次密码是否一致
	if(I("new_pwd")!=I("confirm_new_pwd"))
		$this->error(L("pwd_not_match"));
	//邮箱验证码是否正确
	if(I("verify")!=cookie("email_verify"))
		$this->error(L("invalid_verify"));
	
	$con['mh_id']=$this->merchant_info['mh_id'];
	$data['mh_paypwd']=md5(I("new_pwd").$this->merchant_info['mh_salt']);
	$result=M("merchant")->where($con)->save($data);
	if($result)
		$this->success(L("action_success"));
	else
		$this->error(L("action_failed"));	
}

public function mailTo(){	
	$data['status']=0;
	$data['data']=0;
	
	if(cookie("email_verify")){
		$data['info']=L("repeat_error");
		$this->ajaxReturn($data);		
	}
		
	$verify_code=makePass(5);
	//cookie保存60秒
	cookie("email_verify",$verify_code,70);

	$body="Your code:".$verify_code."<br>".L("change_pwd_verify_code_info")."<p>".$this->web_config['web_domain']."</p>";
	
	$result=parent::postmail($this->merchant_info['mh_email'],"[".L("web_name")."]".L("email_verify_code"),$body);
	if($result){
	$data['status']=1;	
	$data['info']=L("send_success");
	$this->ajaxReturn($data);	
	}else{
	$data['info']=L("send_error");
	$this->ajaxReturn($data);		
	}
	}



protected function withdrawBalance(){
	$oop=M("statement");
	$con['st_mid']=$this->merchant_info['mh_id'];
	$con['st_name']=2;
	$now_day=date("Y-m-d",strtotime("+1 day"));
	$pass_day=date("Y-m-d",strtotime("-".($this->merchant_info['mh_covered_days']-1)." days"));	
	$con['st_time']=array("between",array($pass_day,$now_day));
	$holdMoney=$oop->where($con)->sum('st_amount');
	return number_format($this->merchant_info['mh_balance']-$holdMoney,2,'.','');
}


	
}

